'use strict';

const Controller = require('egg').Controller;

class ModuleController extends Controller {
  // 会获取携带 module_list的 数据
  async getList() {
    const queryObj = this.ctx.request.body.module_name ? this.ctx.request.body : this.ctx.query;
    
    let sql = `select * from module where title like "%${queryObj.module_name}%" or module_name like "%${queryObj.module_name}%"`;

    const data = await this.app.mysql.query(sql);
    for (let i = 0;i < data.length; i++){
      let sql =  `select * from module_list where module_name = "${data[i].module_name}"`
      data[i].moduleList = await this.app.mysql.query(sql)
    }
    
    this.ctx.body = this.ctx.success(data);
  }
  async get() {
    const queryObj = this.ctx.request.body.module_name ? this.ctx.request.body : this.ctx.query;
    const data = await this.app.mysql.select('module', { 
      where: { module_name: String(queryObj.module_name).split(',') },
    });
    for (let i = 0;i < data.length; i++){
      let sql =  `select * from module_list where id = "${data[i].id}"`
      data[i].module_list = await this.app.mysql.query(sql)
    }
    
    this.ctx.body = this.ctx.success(data);
  }

  async add() {
    const queryObj = this.ctx.request.body.module_name ? this.ctx.request.body : this.ctx.query;
    const result = await this.app.mysql.insert('module', queryObj);
    this.ctx.body = this.ctx.success(result);
  }
  
}

module.exports = ModuleController;
